<?php
include_once "BaseDAO.php";
include_once $_SERVER ['DOCUMENT_ROOT'] . '/ad/sys/com/model/util/plog/classes/plog.php';

class AppInfoDAO extends BaseDAO {
	
	public function __construct() {
		parent::__construct ();
		$this->log = Plog::factory ( __FILE__ );
	}
	
	public function getAppInfoDetail($appId) {
		$sql = "select * from appinfo where id='$appId'";
		$result = @mysql_query ( $sql );
		return $this->toArray ( $result );
	
	}
	public function getAppInfoDetailByMd5key($md5key) {
		$sql = "select * from appinfo where md5key='$md5key'";
		$result = @mysql_query ( $sql );
		return $this->toArray ( $result );
	
	}
	public function insert($appinfo) {
		$sql = $this->genInserSQL ( "appinfo", $appinfo );
		$this->log->debug ( "新增appinfo :" . $sql );
		$result = @mysql_query ( $sql );
		return $result;
	}
	
	public function updateAppInfo($id, $name, $adcategorId, $description, $appType,$status, $testAction) {
		
		$sql = "update appinfo set  
		  			 name='$name',
		  			 adcategorId=$adcategorId,
		  			 description='$description',
		  			 appType=$appType ,
		  			 status=$status,
		  			 testAction=$testAction 
		  			 where id=$id
		  ";
		$result = mysql_query ( $sql );
		
		if($status!='1')
		{
		$sql = "update appinfo set  
		  			 
		  			 startTime=now()
		  			 where id=$id
		  ";
		 mysql_query ( $sql );
			
		}
		return $result;
	}
	
	/**
	 * 查詢帳號 下的 帳單管理
	 * Enter description here ...
	 * @param unknown_type $account
	 */
	public function getAppInfoList($start, $limit, $account) {
		$sql = "select count(id) from ad where account='$account'";
		$count = mysql_fetch_row ( mysql_query ( $sql ) );
		
		$sql = "
			select date_format(createTime,'%Y/%m/%d') as createtime,
	        name,
	        id,
	        apptype,
	        status,
	        md5key
			from appinfo
			where account='$account'
			order by createtime desc  limit $start,$limit
		";
		//		echo $sql;
		//where account = '$account' 
		$reuslt = mysql_query ( $sql ) or die ( mysql_error () );
		$item = $this->toArray ( $reuslt );
		
		return array ("count" => $count [0], "item" => $item );
	}
	/**
	 * 查詢帳號 下的 帳單管理
	 * Enter description here ...
	 * @param unknown_type $account
	 */
	public function getAppInfo($id) {
		$sql = "
			select date_format(createTime,'%Y/%m/%d') as createtime,
	        name,
	        id,
	        adcategorId,
	        description,
	        appType,
	        md5key,
   	        status,
         	testAction,
         	Account
			from appinfo
			where id=$id
		";
		//where account = '$account' 
		//		echo "---->$sql<------";
		$reuslt = mysql_query ( $sql ) or die ( mysql_error () );
		$item = $this->toArray ( $reuslt );
		
		return array ($item [0] );
	}
	/**
	 * 查詢帳號 下的 帳單管理
	 * Enter description here ...
	 * @param unknown_type $account
	 */
	public function getAppInfoByName($name) {
		$sql = "
			select date_format(createTime,'%Y/%m/%d') as createtime,
	        name,
	        id,
	        adcategorId,
	        description,
	        appType,
	        md5key,
   	        status,
         	testAction,
         	Account
			from appinfo
			where name='$name'
		";
		//where account = '$account' 
		//		echo "---->$sql<------";
		$reuslt = mysql_query ( $sql ) or die ( mysql_error () );
		$item = $this->toArray ( $reuslt );
		
		return array ($item [0] );
	}
	/**
	 * 查詢帳號 下的 帳單管理
	 * Enter description here ...
	 * @param unknown_type $account
	 */
	public function getAppInfoByMd5key($md5key) {
		$sql = "
			select date_format(createTime,'%Y/%m/%d') as createtime,
	        name,
	        id,
	        adcategorId,
	        description,
	        appType,
	        md5key,
   	        status,
         	testAction,
         	Account
			from appinfo
			where md5key='$md5key'
		";
		//where account = '$account' 
		//		echo "---->$sql<------";
		$reuslt = mysql_query ( $sql ) or die ( mysql_error () );
		$item = $this->toArray ( $reuslt );
		
		return array ($item [0] );
	}
	public function getAppInfoRate($account, $start, $limit) {
		$date = Util::getNowDate ( 'Y/m/d' );
		
		$sql = "
			SELECT d.income, d.name, d.id, d.startTime, c.incomesum
			FROM (
				select b.id,b.name,b.startTime ,e.income from appinfo b 
				left join (
					select a.appid,sum(a.paymoney) income from clickedad a 
					where DATE_FORMAT( a.createtime,  '%Y%m%d' )  = DATE_FORMAT( '$date',  '%Y%m%d')
						group by a.appid,DATE_FORMAT( a.createtime,  '%Y%m%d' )
 					 ) e
					 on e.appid=b.id where b.account = '$account' ) d
				left JOIN (
					SELECT appid, SUM( paymoney) incomesum FROM clickedad group by appid)c
					 ON d.id = c.appid
			LIMIT  $start , $limit";
//		echo $sql;
		//where account = '$account' 
		$reuslt = mysql_query ( $sql ) or die ( mysql_error () );
		
		$item = $this->toArray ( $reuslt );
		//		if(count($item)<1)
		//		{
		//		return array("appinfoid"=>'1',"income"=>0,"rate1"=>0,"rate2"=>0,"name"=>"name");
		//		}
		return array ($item );
	}

}

?>